package cars

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/cars"
	carsReq "github.com/flipped-aurora/gin-vue-admin/server/model/cars/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/garage"
	"github.com/flipped-aurora/gin-vue-admin/server/model/parkingSpace"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CarApi struct {
}

var carService = service.ServiceGroupApp.CarsServiceGroup.CarService

// CreateCar 创建car表
// @Tags Car
// @Summary 创建car表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body cars.Car true "创建car表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /car/createCar [post]
func (carApi *CarApi) CreateCar(c *gin.Context) {
	var car cars.Car
	err := c.ShouldBindJSON(&car)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := carService.CreateCar(&car); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteCar 删除car表
// @Tags Car
// @Summary 删除car表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body cars.Car true "删除car表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /car/deleteCar [delete]
func (carApi *CarApi) DeleteCar(c *gin.Context) {
	var car cars.Car
	err := c.ShouldBindJSON(&car)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := carService.DeleteCar(car); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteCarByIds 批量删除car表
// @Tags Car
// @Summary 批量删除car表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除car表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /car/deleteCarByIds [delete]
func (carApi *CarApi) DeleteCarByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := carService.DeleteCarByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateCar 更新car表
// @Tags Car
// @Summary 更新car表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body cars.Car true "更新car表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /car/updateCar [put]
func (carApi *CarApi) UpdateCar(c *gin.Context) {
	var car cars.Car
	err := c.ShouldBindJSON(&car)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := carService.UpdateCar(car); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindCar 用id查询car表
// @Tags Car
// @Summary 用id查询car表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query cars.Car true "用id查询car表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /car/findCar [get]
func (carApi *CarApi) FindCar(c *gin.Context) {
	var car cars.Car
	err := c.ShouldBindQuery(&car)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if recar, err := carService.GetCar(car.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {

		var data cars.CarRes

		//db := global.GVA_DB.Model(&parkingSpace.ParkingSpace{})
		var parkingSpaceData parkingSpace.ParkingSpace
		global.GVA_DB.Model(&parkingSpace.ParkingSpace{}).Where("id = ?", recar.ParkingSpaceId).First(&parkingSpaceData)
		fmt.Println(parkingSpaceData)
		var garageData garage.Garage
		global.GVA_DB.Model(&garage.Garage{}).Where("id = ?", parkingSpaceData.GarageId).First(&garageData)
		data = cars.CarRes{
			GVA_MODEL: global.GVA_MODEL{
				ID:        recar.ID,
				CreatedAt: recar.CreatedAt,
				UpdatedAt: recar.UpdatedAt,
			},
			GarageName:       garageData.Name,
			ParkingSpaceName: parkingSpaceData.ParkingSpaceNum, //garageId字段
			IsLease:          recar.IsLease,
			CarNumber:        recar.CarNumber, //车位面积
			OwnerName:        recar.OwnerName, //楼层
			Sex:              recar.Sex,       //车位类型
			Mobile:           recar.Mobile,
		}
		fmt.Println(data)
		response.OkWithData(gin.H{"recar": data}, c)
	}
}

// GetCarList 分页获取car表列表
// @Tags Car
// @Summary 分页获取car表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query carsReq.CarSearch true "分页获取car表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /car/getCarList [get]
func (carApi *CarApi) GetCarList(c *gin.Context) {
	var pageInfo carsReq.CarSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := carService.GetCarInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {

		var datas []*cars.CarRes
		for _, v := range list {
			//db := global.GVA_DB.Model(&parkingSpace.ParkingSpace{})
			var parkingSpaceData parkingSpace.ParkingSpace
			global.GVA_DB.Model(&parkingSpace.ParkingSpace{}).Where("id = ?", v.ParkingSpaceId).First(&parkingSpaceData)
			fmt.Println(parkingSpaceData)
			var garageData garage.Garage
			global.GVA_DB.Model(&garage.Garage{}).Where("id = ?", parkingSpaceData.GarageId).First(&garageData)
			datas = append(datas, &cars.CarRes{
				GVA_MODEL: global.GVA_MODEL{
					ID:        v.ID,
					CreatedAt: v.CreatedAt,
					UpdatedAt: v.UpdatedAt,
				},
				GarageName:       garageData.Name,
				ParkingSpaceName: parkingSpaceData.ParkingSpaceNum, //garageId字段
				IsLease:          v.IsLease,
				CarNumber:        v.CarNumber, //车位面积
				OwnerName:        v.OwnerName, //楼层
				Sex:              v.Sex,       //车位类型
				Mobile:           v.Mobile,
			})

		}

		response.OkWithDetailed(response.PageResult{
			List:     datas,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
